package com.cy.metrics;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import org.testng.annotations.Test;

import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * https://metrics.dropwizard.io/3.1.0/
 * @author yue.cao
 * @since 12-29-2020
 */
public class MeterDemo {


	@Test(description = "Meter 工具  用于计次")
	public void testMeter() throws InterruptedException {
		MetricRegistry registry = new MetricRegistry();
		Meter tps = registry.meter("tps");
		Meter size = registry.meter("size");

		ConsoleReporter reporter = ConsoleReporter
				.forRegistry(registry)
				.convertRatesTo(TimeUnit.MINUTES)
				.convertDurationsTo(TimeUnit.MINUTES).build();

		reporter.start(10, TimeUnit.SECONDS);
		while (true) {
			tps.mark();
			size.mark(ThreadLocalRandom.current().nextInt(1000));
			TimeUnit.SECONDS.sleep(1);
		}
	}


}
